package com.zib.bilibili.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.zib.bilibili.R;
import com.zib.bilibili.bean.TuijianBean;
import com.zib.bilibili.customview.MyGirdView;
import com.zib.bilibili.utils.ImageLoader;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * ----------BigGod be here!----------/
 * ***┏┓******┏┓*********
 * *┏━┛┻━━━━━━┛┻━━┓*******
 * *┃             ┃*******
 * *┃     ━━━     ┃*******
 * *┃             ┃*******
 * *┃  ━┳┛   ┗┳━  ┃*******
 * *┃             ┃*******
 * *┃     ━┻━     ┃*******
 * *┃             ┃*******
 * *┗━━━┓     ┏━━━┛*******
 * *****┃     ┃神兽保佑*****
 * *****┃     ┃代码无BUG！***
 * *****┃     ┗━━━━━━━━┓*****
 * *****┃              ┣┓****
 * *****┃              ┏┛****
 * *****┗━┓┓┏━━━━┳┓┏━━━┛*****
 * *******┃┫┫****┃┫┫********
 * *******┗┻┛****┗┻┛*********
 * ━━━━━━神龙出没━━━━━━
 * <p/>
 * 项目名称：@string/app_name
 * 类描述：
 * 创建人：jackie
 * 创建时间：2016/7/9
 * 修改人：jackie
 * 修改时间：2016/7/9
 * 修改备注：
 */
public class TuijianFragmentAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context = null;
    private List<TuijianBean.ResultBean> list = null;
    private LayoutInflater inflater = null;

    private static final int TYPE_STATE_1 = 1;
    private static final int TYPE_STATE_4 = 4;
    private static final int TYPE_STATE_6 = 6;


    private TuijianGridViewAdapter adapter=null;


    public TuijianFragmentAdapter(Context context, List<TuijianBean.ResultBean> list) {
        this.context = context;
        this.list = list;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    @Override
    public int getItemViewType(int position) {
        int size = list.get(position).getBody().size();
        if (size == 1) {
            return TYPE_STATE_1;
        } else if (size == 4) {
            return TYPE_STATE_4;
        }
        return TYPE_STATE_4;


    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_STATE_1) {
            ViewHolder1 mHolder = null;
            View view = inflater.inflate(R.layout.item_recyclerview_tuijian2, parent, false);
            return new ViewHolder2(view);
        } else if (viewType == TYPE_STATE_4) {
            ViewHolder1 mHolder = null;
            View view = inflater.inflate(R.layout.item_recyclerview_tuijian, parent, false);
            return new ViewHolder1(view);
        } else {
            ViewHolder1 mHolder = null;
            View view = inflater.inflate(R.layout.item_recyclerview_tuijian, parent, false);
            return new ViewHolder1(view);
        }

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {

        List<TuijianBean.ResultBean.BodyBean> list_body = list.get(position).getBody();
        TuijianBean.ResultBean.HeadBean list_head = list.get(position).getHead();

        if (holder instanceof ViewHolder1) {
            ((ViewHolder1) holder).txtHeaderTitle.setText(list_head.getTitle() + "");

            try{
                ((ViewHolder1) holder).txtHeaderCount.setVisibility(View.GONE);
                if (list_head.getCount().length()>0){
                    ((ViewHolder1) holder).txtHeaderCount.setVisibility(View.VISIBLE);
                    ((ViewHolder1) holder).txtHeaderCount.setText("当前"+list_head.getCount()+"个直播");
                 }else {
                    ((ViewHolder1) holder).txtHeaderCount.setVisibility(View.GONE);
                }
            }catch (Exception e){}

            adapter=new TuijianGridViewAdapter(context,list_body);
            ((ViewHolder1) holder).gvBodyTuijian.setAdapter(adapter);

        }else if (holder instanceof ViewHolder2){
            ImageLoader.getInstance().displayImage(context,list.get(position).getBody().get(0).getCover(),((ViewHolder2) holder).imgHeaderCover);

        }

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    static class ViewHolder1 extends RecyclerView.ViewHolder {
        @Bind(R.id.img_header_icon)
        ImageView imgHeaderIcon;
        @Bind(R.id.txt_header_title)
        TextView txtHeaderTitle;
        @Bind(R.id.txt_header_count)
        TextView txtHeaderCount;
        @Bind(R.id.txt_header_more)
        TextView txtHeaderMore;
        @Bind(R.id.gv_body_tuijian)
        MyGirdView gvBodyTuijian;

        ViewHolder1(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }


    public void reloadRecyclerView(List<TuijianBean.ResultBean> _list, boolean isClear) {
        if (isClear) {
            list.clear();
        }
        list.addAll(_list);
        notifyDataSetChanged();
    }

    static class ViewHolder2 extends RecyclerView.ViewHolder {
        @Bind(R.id.img_header_icon)
        ImageView imgHeaderIcon;
        @Bind(R.id.img_header_cover)
        ImageView imgHeaderCover;

        ViewHolder2(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }
}
